import urllib.request
import urllib.parse
import ssl

ssl._create_default_https_context = ssl._create_unverified_context
def requestFun(page):
    url = "https://m.douban.com/rexxar/api/v2/movie/recommend?="
    # 是get请求
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
        "Cookie": "bid=OtXjpIFOD54; ll=118159; __utmc=30149280; __utma=30149280.2112683896.1686489251.1686589032.1686835354.3; __utmz=30149280.1686835354.3.3.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; __utmt=1; __utmb=30149280.1.10.1686835354; ap_v=0,6.0; __gads=ID=10cc3d656d49b384-221230f5a4b40063:T=1686489252:RT=1686835372:S=ALNI_MaybSoe94NtdY7cOlOGiW0zdzuNKw; __gpi=UID=00000c49e64a33eb:T=1686489252:RT=1686835372:S=ALNI_Mave4_2LgJI5ZRpKohe7K89tB76IA"
    }
    start = (page - 1) * 20
    data = {
        "refresh": 0,
        "start": start,
        "count": 20,
        "selected_categories": {"类型": "喜剧"},
        "uncollect": False,
        "tags": "喜剧"
    }
    new_data = urllib.parse.urlencode(data)
    request = urllib.request.Request(url=url+new_data, headers=headers)

    req = urllib.request.urlopen(request)
    content = req.read().decode('utf-8')
    print(content)



if __name__ == '__main__':
    page_start = int(input('请输入起始页：'))
    page_end = int(input('请输入结束页：'))
    for page in range(page_start, page_end + 1):
        requestFun(page)